﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SubSonic;
using ExtAspNet;


namespace TCMSystem.admin
{
    public partial class user_new : PageBase
    {
        private static readonly log4net.ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        #region Module

        /// <summary>
        /// 当前页面所属的模块
        /// </summary>
        public override ModuleType Module
        {
            get
            {
                return ModuleType.CoreUser;
            }
        }

        #endregion


        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            // 权限检查
            if (!CheckPowerNew())
            {
                CheckPowerFailWithPage();
                return;
            }

            btnClose.OnClientClick = ActiveWindow.GetConfirmHidePostBackReference();


            // 绑定角色下拉列表
            BindDDLRole();

            // 绑定部门下拉列表
            BindDDLDept();
        }

        private void BindDDLRole()
        {
            TblMstRoleCollection items = new Select().From<TblMstRole>()
                //.OrderAsc(TblMstRole.SortIndexColumn.ColumnName)
                .ExecuteAsCollection<TblMstRoleCollection>();

            ddlRole.DataTextField = "Name";
            ddlRole.DataValueField = "Id";
            ddlRole.DataSource = items;
            ddlRole.DataBind();

            ddlRole.Items.Insert(0, new ExtAspNet.ListItem("--请选择--", "-1"));
            ddlRole.SelectedValue = "-1";
        }

        private void BindDDLDept()
        {
            TblMstDeptCollection items = new Select().From<TblMstDept>()
                .OrderAsc(TblMstDept.SortIndexColumn.ColumnName)
                .ExecuteAsCollection<TblMstDeptCollection>();
            List<CustomDept> mys = CustomDeptHelper.GetMyDeptCollection(items);

            // 绑定到下拉列表（启用模拟树功能和不可选择项功能）
            ddlDept.EnableSimulateTree = true;
            ddlDept.DataTextField = "Name";
            ddlDept.DataValueField = "Id";
            ddlDept.DataSimulateTreeLevelField = "TreeLevel";
            ddlDept.DataEnableSelectField = "Enabled";
            ddlDept.DataSource = mys;
            ddlDept.DataBind();

            ddlDept.Items.Insert(0, new ExtAspNet.ListItem("--请选择--", "-1"));
            ddlDept.SelectedValue = "-1";
        }

        #endregion

        #region Events

        private void SaveItem()
        {
            TblMstUser item = new TblMstUser();
            item.Name = tbxName.Text.Trim();
            item.RealName = tbxRealName.Text.Trim();
            item.Gender = ddlGender.SelectedValue;
            item.Email = tbxEmail.Text.Trim();
            item.CellPhone = tbxCellPhone.Text.Trim();
            item.OfficePhone = tbxOfficePhone.Text.Trim();
            item.HomePhone = tbxHomePhone.Text.Trim();
            item.Remark = tbxRemark.Text.Trim();
            item.Enabled = cbxEnabled.Checked;
            item.RoleId = Convert.ToInt32(ddlRole.SelectedValue);
            item.DeptId = Convert.ToInt32(ddlDept.SelectedValue);
            //item.CreateTime = DateTime.Now;
            item.Save(User.Identity.Name);
        }

        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            SaveItem();

            //Alert.Show("添加成功！", String.Empty, ActiveWindow.GetHidePostBackReference());
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        #endregion

    }
}
